package visualizer.google;

public class GoogleServiceFactory {
	
	private final static String REFERER = "http://users.tkk.fi/saukkee";
	private final static String APIKEY = "ABQIAAAA_yYxdXCwx5vSSwr2mFtqvRTJR5AO0CyI4SJpupZwIPzQ2flNpxRaTx0NMWIVBNWE3BGF64XUI2MIZg";
	private IpAddressRetriever ip;
	
	public GoogleServiceFactory(IpAddressRetriever ip)
	{
		this.ip = ip;
	}
	
	protected JsonRequest createJsonRequest()
	{
		return new JsonRequest(REFERER);
	}
	
	protected GoogleRequestUrlBuilder createUrlBuilder(String service, String category)
	{
		GoogleRequestUrlBuilder b = new GoogleRequestUrlBuilder();
		b.setService(service)
		 .setCategory(category)
		 .set("key", APIKEY);
		
		String ip = this.ip.get();
		if (ip != null) {
			b.set("userip", ip);
		}
		
		return b; 
	}
	
	public GoogleService create()
	{
		return create("web");
	}
	
	public GoogleService create(String category)
	{
		return create("search", category);
	}
	
	public GoogleService create(String service, String category)
	{
		 return new GoogleService(createJsonRequest(), createUrlBuilder(service, category));
	}
}
